﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineGameClient.ViewModel;
using System.Diagnostics;

namespace OnlineGameClient.View
{
    public partial class GameRoomDetailView : UserControl
    {
        public GameRoomDetailView()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(GameRoomDetailView_Loaded);
        }

        void GameRoomDetailView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var vm = this.DataContext as GameRoomViewModel;

                configview2.GameInfo = vm.CurrentGameRoom.GameInfo;
                configview2.SetGameConfig(vm.CurrentGameRoom.GameConfig);

                LoadGameInformation();
                vm.GameImporter.ImportChanged += new EventHandler(GameImporter_ImportChanged);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("error occured: " + ex);
            }
        }

        void LoadGameInformation()
        {
            var vm = this.DataContext as GameRoomViewModel;
            var gameimporter = vm.GameImporter;
            var game = gameimporter.Games.FirstOrDefault(p => p.Metadata.IdentityName == vm.CurrentGameRoom.GameInfo.IdentityName);
            if (game != null)
            {
                tabControl1.SelectedItem = tabItem2;
                sv1.Content = game.Value.CreateWelcomeScreen();
            }
        }

        void GameImporter_ImportChanged(object sender, EventArgs e)
        {
            LoadGameInformation();
        }

        private void GameConfigView2_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
